package com.jiangjiang.eshop.commodity.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.common.collect.Lists;
import com.jiangjiang.eshop.commodity.dao.GoodsPictureMapper;
import com.jiangjiang.eshop.commodity.domain.GoodsPictureEntity;
import com.jiangjiang.eshop.common.constant.PathType;
import com.jiangjiang.eshop.common.util.FileUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.sql.Wrapper;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @ClassName GoodsPictureService
 * @Description TODO
 * @Author jhw
 * @Date 2021/8/2 17:22
 * @Version 1.0
 **/
@Service
public class GoodsPictureService extends ServiceImpl<GoodsPictureMapper, GoodsPictureEntity> {


    @Value("${commodity.goods.image.upload.picture.path.type}")
    private String uploadDirPathType;
    @Value("${commodity.goods.image.upload.picture.path}")
    private String uploadDirPath;


    /**
     * 根据商品id进行数据删除
     *
     * @param goodsId 商品id
     */
    public void removeByGoodsId(Long goodsId) {
        LambdaQueryWrapper<GoodsPictureEntity> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(GoodsPictureEntity::getGoodsId, goodsId);
        this.remove(wrapper);
    }

    /**
     * 批量上传商品图片
     *
     * @param goodsId  商品id
     * @param pictures 图片集合
     * @return 处理结果
     */
    public Boolean batchUpload(Long goodsId, MultipartFile[] pictures) throws Exception {

        List<String> pathUrls = Lists.newArrayList();
        for (MultipartFile picture : pictures) {
            String pathUrl = uploadPicture(picture);
            pathUrls.add(pathUrl);
        }

        if (CollectionUtils.isNotEmpty(pathUrls)) {
            List<GoodsPictureEntity> pictureEntities = pathUrls.stream().map(url ->
                    GoodsPictureEntity.builder()
                            .goodsId(goodsId)
                            .picturePath(url)
                            .build())
                    .collect(Collectors.toList());
            return this.saveBatch(pictureEntities);
        }

        return false;
    }

    /**
     * 上传图片数据
     *
     * @param picture 图片
     * @return
     */
    private String uploadPicture(MultipartFile picture) throws Exception {
        String realUploadDirPath = getRealUploadDirPath();
        return FileUtils.uploadFile(picture, realUploadDirPath);
    }

    /**
     * 获取真实的图片上传路径
     *
     * @return
     */
    private String getRealUploadDirPath() throws Exception {
        String realUploadDirPath = uploadDirPath;
        if (PathType.RELATIVE.equals(uploadDirPathType)) {
            realUploadDirPath = FileUtils.getPathByRelative(uploadDirPath);
        }

        return realUploadDirPath;
    }

    /**
     * 根据商品id,查询商品图片集合数据
     *
     * @param goodsId 商品id
     * @return 查询结果
     */
    public List<String> queryPicUrlByGoodsId(Long goodsId) {
        LambdaQueryWrapper<GoodsPictureEntity> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(GoodsPictureEntity::getGoodsId, goodsId);
        return this.list(wrapper).stream().map(GoodsPictureEntity::getPicturePath).collect(Collectors.toList());
    }

    /**
     * 根据商品id,批量删除商品图片
     * @param goodsId 商品id
     * @return 处理结果
     */
    public Boolean batchRemoveByGoodsId(Long goodsId) throws Exception {
        List<String> urls = this.queryPicUrlByGoodsId(goodsId);
        for (String url : urls) {
            FileUtils.deleteFile(url);
        }
        LambdaQueryWrapper<GoodsPictureEntity> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(GoodsPictureEntity::getGoodsId, goodsId);
        return this.remove(wrapper);
    }
}
